var express = require('express');
var router = express.Router();
const jwt = require('jsonwebtoken');
const { verifyToken, judgeToken, decodeToken } = require('../../tool/jwt')
const { setDate } = require('../../tool/date')

//验证token方法
const tokenvalidate = (req,res,next)=>{
    try {
      const token = req.headers.token;
      const v = verifyToken(token,'shop_key')
      if(v != null){
        const v2 = judgeToken(token,'shop_key')
        if(v2 ==0){
          next()
        }else{
          return res.send({data:{msg:'token已过期',code:'500'}})
        }
  
      }else{
        return res.send({data:{msg:'token已过期',code:'500'}})
      }
      
    } catch (error) {
      console.log(error)
      res.status(500).send('Error');

    }
  }

module.exports = (pool) => {
    //查询
    router.get('/getlist/:current',async(req,res)=>{
        try {
            let current = (req.params.current -1)*10
            const connection = await pool.promise().getConnection();
            //分页
            const result = await connection.query(`select orders.*,user.nickname from orders join user on user.id = orders.uid ORDER BY orders.id  desc LIMIT ${current},10`)
            connection.release()
            if(result.length>0){
                res.send({data:{msg:"查询成功",code:200,list:result[0]}})
            }else{
                res.send({data:{msg:"查询失败",code:500}})
            }
        } catch (error) {
            console.error(error);
            res.status(500).send({ data: { msg: "服务异常", code: 500 } });
      
        }
    })
    //修改
    router.post('/update',async(req,res)=>{
      try {
        const {order_status,id} = req.body
        const connection = await pool.promise().getConnection();
        const result = await connection.query('update orders set order_status=? where id=?',[order_status,id])
        connection.release()
        if(result[0].affectedRows>0){
            res.send({data:{msg:"修改成功",code:200}})
        }else{
            res.send({data:{msg:"修改失败",code:500}})
        }
      } catch (error) {
        console.error(error);
        res.status(500).send({ data: { msg: "服务异常", code: 500 } });
  
      }
    })
    //删除
    router.get('/delete/:id',async(req,res)=>{
      try {
        const {id} = req.params
        const connection = await pool.promise().getConnection();
        const result = await connection.query('delete from orders where id=?',[id])
        connection.release()
        if(result[0].affectedRows>0){
            res.send({data:{msg:"删除成功",code:200}})
        }else{
            res.send({data:{msg:"删除失败",code:500}})
        }
      } catch (error) {
        console.error(error);
        res.status(500).send({ data: { msg: "服务异常", code: 500 } });
  
      }
    })
    //获取总条数
    router.get('/gettotal',async(req,res)=>{
      try {
        const connection = await pool.promise().getConnection();
        var total = await connection.query(`SELECT count(*) as total from orders`)
        connection.release()
        res.send(total[0][0])
      } catch (error) {
        console.error(error);
        res.status(500).send({ data: { msg: "服务异常", code: 500 } });
  
      }
    })


    //返回路由
    return router
}